import isEmpty from 'loadsh/isEmpty'
import {
  GetConsumablesManagementInfo,
  PostConsumablesManagementInfo
} from '/constData/requestFun/_httpRequest';
import {
  ConsumableManagementDeleteData,
  GetConsumableManagement,
  GetConsumableManagementById,
  GetDataConsumableManagement,
  PostConsumableManagement,
  PutFileConsumableManagement
} from '/constData/requestFun/_teseRequest';
Page({
  data: {
    item: 1,
    min: null,
    max: new Date('2033/12/02'),
    defaultDate: new Date(),
    consumablesinfo: {
      CorpId: '', //申请人
      ConsumableId: '',
      ConsumableLevel: '',
      Name: '',
      Count: '', //耗材名称
      Usage: '', //使用目的
    },
    NameData: [],
    TempData: [],
    ReceiveData: [],
  },
  OnNameChange(value) {
    let data = this.data.TempData.filter((it) => {
      if (it.name === value) {
        return true
      }
    })
    if (!(data.length > 0)) data = [{
      level: ''
    }]
    data = data[0].level

    this.setData({
      consumablesinfo: {
        ...this.data.consumablesinfo,
        Name: value,
        ConsumableLevel: data
      }
    })
  },
  OnCountChange(value) {
    this.setData({
      consumablesinfo: {
        ...this.data.consumablesinfo,
        Count: value
      }
    })
  },
  OnUsageChange(value) {
    this.setData({
      consumablesinfo: {
        ...this.data.consumablesinfo,
        Usage: value
      }
    })
  },
  Delete(e) {
    const id = e.currentTarget.dataset.id
    ConsumableManagementDeleteData(id, (res) => {
      my.alert({
        title: '撤销成功',
      });
      //刷新数据
      this.setData({
        ReceiveData: this.data.ReceiveData.filter((it) => {
          return it.item1.id !== id
        })
      })
    }, (err) => {
      my.alert({
        title: '撤销失败',
      });
    })
  },
  //获取数据
  onLoad() {
    my.getStorage({
      key: 'userInfo',
      success: (storage) => {
        GetDataConsumableManagement((res) => {
          let NameData = [];
          let CountData = [];
          res.data.forEach(item => {
            NameData.push(item.name);
            CountData.push(item.count.toString());
          });
          this.setData({
            NameData: NameData,
            TempData: res.data,
            consumablesinfo: {
              ...this.data.consumablesinfo,
              CorpId: storage.data.result.userid
            },
          });
          GetConsumableManagement((res) => {
            this.setData({
              ReceiveData: res.data.reverse(),
            })
          })
        })
      }
    })
  },
  // 上传数据
  onSubmit(e) {
    console.log('data', this.data.consumablesinfo);
    PutFileConsumableManagement(
      this.data.consumablesinfo.Name,
      this.data.consumablesinfo.Count,
      this.data.consumablesinfo.CorpId,
      this.data.consumablesinfo.Usage,
      this.data.consumablesinfo.ConsumableLevel,
      (res) => {
        my.alert({
          title: '提交成功',
        });
        GetConsumableManagement((res) => {
          this.setData({
            ReceiveData: res.data.reverse(),
          })
        })
        this.setData({
          consumablesinfo: {
            ...this.data.consumablesinfo,
            CorpId: '', //申请人
            Name: '',
            Count: '', //耗材名称
            Usage: '', //使用目的
          }
        })
      }
    )
  },

});